App({
    async onLaunch() {
        const {env} = require('./utils/config')
        wx.cloud.init({
            env,
            traceUser: true,
        });

        //获取导航栏数据并保存到缓存中
        const {
            statusBarHeight,
            platform
        } = wx.getSystemInfoSync()
        const {
            top,
            height
        } = wx.getMenuButtonBoundingClientRect()

        // 状态栏高度
        wx.setStorageSync('statusBarHeight', statusBarHeight)
        // 胶囊按钮高度 一般是32 如果获取不到就使用32
        wx.setStorageSync('menuButtonHeight', height ? height : 32)

        // 判断胶囊按钮信息是否成功获取
        if (top && top !== 0 && height && height !== 0) {
            const navigationBarHeight = (top - statusBarHeight) * 2 + height
            // 导航栏高度
            wx.setStorageSync('navigationBarHeight', navigationBarHeight)
        } else {
            wx.setStorageSync(
                'navigationBarHeight',
                platform === 'android' ? 48 : 40
            )
        }

        //调用云函数获取OPENID
        let loginInfo = await wx.cloud.callFunction({
            name: 'getOpenId'
        })
        wx.setStorageSync('openid', loginInfo.result.openid)

        //登陆状态初始值为 false
        wx.setStorageSync('logged', false)
        //检查登录
        this.inspectLogin();

    },
    onShow() {
        wx.hideTabBar({
            animation: false
        })
    },
    //检查是否登陆过
    inspectLogin() {
        const {getUser} = require('/utils/request')
        getUser(wx.getStorageSync('openid')).then(res => {
            if (res.data.length > 0) {
                wx.setStorageSync('userInfo', res.data[0])
                wx.setStorageSync('logged', true)
            } else {
                wx.setStorageSync('logged', false)
            }
        })
    },
    globalData: {}
})